/**
 * storage封装
 */

const STORAGE_KEY = 'cendxia';
export default{
    // 存储值
    setItem(key, value, module_name){
        if(module_name){
            let val = this.getItem(module_name);
            val[key] = value;
            this.setItem(module_name, val);
        }else{
            let val = this.getStorage();
            val[key] = value;
            window.localStorage.setItem(STORAGE_KEY, JSON.stringify(val));
            // sessionStorage
        }
    },
    // 获取某一个模块下面的属性
    getItem(key, module_name){
        if(module_name){
            let val = this.getItem(module_name);
            if(val) return val[key];
        }
        return this.getStorage()[key];
    },
    getStorage(){
        return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}');
    },
    // 删除
    clear(key, module_name){
        let val = this.getStorage();
        if(module_name){
            window.localStorage.removeItem(module_name.key);
        }else{
            console.log(3333);
            window.localStorage.removeItem(key)
        }
        // 重新写入值
        this.setItem(val);
    }
}
